package com.example.loopyserver.utils;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;

import java.nio.ByteBuffer;


public class UIDUtils {
    private static final Snowflake SNOWFLAKE = IdUtil.getSnowflake();

    private UIDUtils() {
    }

    public static Long next() {
        return SNOWFLAKE.nextId();
    }

    /**
     * 将Long类型的雪花ID通过Base64转为长度为11的URL安全的字符串
     **/
    public static String nextStr() {
        ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES);
        buffer.putLong(next());
        return Base64.encodeUrlSafe(buffer.array());
    }
}
